package com.weddingee.dao.home;

import com.weddingee.dao.base.BaseDao;
import com.weddingee.exception.DBException;
import com.weddingee.exception.DaoException;
import com.weddingee.model.PhotoGroup;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.util.List;
import java.util.ArrayList;

public class PhotoGroupDao extends BaseDao{
	
	

	// save
	public PhotoGroup save(PhotoGroup photoGroup)throws DaoException {
		Connection conn = null;
		PreparedStatement pstm  = null;
			try {
				conn = getConn();
				String sql = "insert into photoGroup values(null,?,?,?)";
				pstm = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				//USERID
				pstm.setLong(1, photoGroup.getUserId());
				//NAME
				pstm.setString(2, photoGroup.getName());
				//URL
				pstm.setString(3,  photoGroup.getUrl());
				
					
				
				
				int res = pstm.executeUpdate();
				if( res > 0){
					ResultSet rset = pstm.getGeneratedKeys();
					rset.next();
					photoGroup.setId(rset.getLong(1));
					return photoGroup;
				}
				
				return null;
			} catch (Exception e) {
				e.printStackTrace();
				throw new DaoException("error");
			} finally{
				try {
					closeStatement(pstm);
					closeConn(conn);
				} catch (DBException e) {
					e.printStackTrace();
				}
			}
		
	}

	// list
	public List<PhotoGroup> listPhotoGroup(Long userId) {
		Connection conn = null;
		PreparedStatement pstm  = null;
		
		 List<PhotoGroup> photoGroupList = new ArrayList<PhotoGroup>();
			try {
				conn = getConn();
				String sql = "SELECT * FROM photoGroup where USERID = ? order by id asc";
				pstm = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				pstm.setLong(1, userId);
				ResultSet rset = pstm.executeQuery();
				int i=0;
				while(rset.next())
				{
				PhotoGroup photoGroup = new PhotoGroup();
				photoGroup.setId(rset.getLong(1));
				photoGroup.setUserId(rset.getLong(2));
				photoGroup.setName(rset.getString(3));
				photoGroup.setUrl(rset.getString(4));
				
				
				photoGroupList.add(i,photoGroup);
				i++;
				}
			    return photoGroupList;
				
				
			} catch (Exception e) {
				e.printStackTrace();
				throw new DaoException("error",e);
			} finally{
				try {
					closeStatement(pstm);
					closeConn(conn);
				} catch (DBException e) {
					e.printStackTrace();
				}
			}
		
		
		
	}
    
	//update
	public PhotoGroup update (PhotoGroup photoGroup){
		Connection conn = null;
		PreparedStatement pstm  = null;
			try {
				conn = getConn();
				String sql = "update photoGroup set URL=? , USERID=? , GROUPNAME=?  where id= ?";
				pstm = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				//PICURL
				pstm.setString(1, photoGroup.getUrl());
				//PHOTOGROUP
				pstm.setLong(2,photoGroup.getUserId());
				//NAME
				pstm.setString(3,  photoGroup.getName());	
				//id
				pstm.setLong(4, photoGroup.getId());
				int res = pstm.executeUpdate();
				return photoGroup;

			} catch (Exception e) {
				e.printStackTrace();
				throw new DaoException("error");
			} finally{
				try {
					closeStatement(pstm);
					closeConn(conn);
				} catch (DBException e) {
					e.printStackTrace();
				}
			}
	}

	
	// find
	public PhotoGroup find(Long photoGroupId) {
		Connection conn = null;
		PreparedStatement pstm  = null;
		PhotoGroup photoGroup = new PhotoGroup();
			try {
				conn = getConn();
				String sql = "select * from photoGroup where id= ?";
				pstm = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				
				//photoGroupId
				pstm.setLong(1, photoGroupId);
				
				
				ResultSet rset = pstm.executeQuery();
				rset.next();
				photoGroup.setId(photoGroupId);
				photoGroup.setUserId(rset.getLong(2));
				photoGroup.setName(rset.getString(3));
				photoGroup.setUrl(rset.getString(4));
				return photoGroup;
				
				
			} catch (Exception e) {
				e.printStackTrace();
				throw new DaoException("error",e);
			} finally{
				try {
					closeStatement(pstm);
					closeConn(conn);
				} catch (DBException e) {
					e.printStackTrace();
				}
			}
		
		
	}

	// delete
	public String delete(Long photoGroupId) {
		Connection conn = null;
		PreparedStatement pstm  = null;
			try {
				conn = getConn();
				String sql = "delete from photoGroup where id= ?";
				pstm = conn.prepareStatement(sql,Statement.RETURN_GENERATED_KEYS);
				
				//photoGroupId
				pstm.setLong(1, photoGroupId);
				int res = pstm.executeUpdate();
				return null;
			} catch (Exception e) {
				e.printStackTrace();
				throw new DaoException("error");
			} finally{
				try {
					closeStatement(pstm);
					closeConn(conn);
				} catch (DBException e) {
					e.printStackTrace();
				}
			}
	}
}